package com.leon.androidcoursedesign.adapters;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.adapters.baseAdapter.BaseAdapter;
import com.leon.androidcoursedesign.adapters.baseAdapter.BaseViewHolder;
import com.leon.androidcoursedesign.beans.Note;
import com.leon.androidcoursedesign.ui.views.RoundPopupWindow;
import com.leon.androidcoursedesign.utils.ValueUtils;

import java.util.List;

public class NoteListAdapter extends BaseAdapter<Note> {
    private final List<Note> notes;
    private final Context context;

    public NoteListAdapter(List<Note> notes, Context context) {
        super(notes, context);
        this.notes = notes;
        this.context = context;
    }

    @Override
    public int setLayout() {
        return R.layout.note_list_item;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        Note note = notes.get(position);

        holder
                .setText(R.id.note_list_item_title, note.showName)
                .setText(R.id.note_list_item_saveTime, "保存于：" + ValueUtils.formatTime(note.saveTime))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("note", note);
                        Navigation.findNavController(v).navigate(R.id.action_noteListFragment_to_noteEditFragment, bundle);
                    }
                })
                .setLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        RoundPopupWindow roundPopupWindow = new RoundPopupWindow(context, v);
                        roundPopupWindow
                                .setContentView(R.layout.note_item_popup_window)
                                .setOnClickListener(R.id.note_popWindow_textView_rename, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (onNoteItemListener != null) {
                                            onNoteItemListener.renameNote(note);

                                            roundPopupWindow.dismiss();
                                        }
                                    }
                                })
                                .setOnClickListener(R.id.note_popWindow_textView_delete, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // 删除对应course
                                        if (onNoteItemListener != null) {
                                            onNoteItemListener.deleteNote(note.itemId, note.noteId, position);
                                        }

                                        roundPopupWindow.dismiss();
                                    }
                                })
                                .create();

                        return true;
                    }
                });
    }

    private OnNoteItemListener onNoteItemListener;

    public interface OnNoteItemListener{
        void renameNote(Note note);
        void deleteNote(String itemId, String noteId, int position);
    }

    public void setOnNoteItemListener(OnNoteItemListener onNoteItemListener) {
        this.onNoteItemListener = onNoteItemListener;
    }
}
